#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@author: wind
@contact: 367059791@qq.com
@time: 2022/1/14 9:47
@file: demo_doubleslider.py
@desc:
"""
from PySide2.QtWidgets import QSlider, QApplication, QWidget, QLabel, QVBoxLayout
from PySide2.QtCore import Signal, Qt
from decimal import Decimal


class QDoubleSlider(QSlider):
    floatValueChanged = Signal(float)
    floatRangeChanged = Signal(float, float)

    def __init__(self, *args, **kwargs):
        super(QDoubleSlider, self).__init__(*args, **kwargs)
        self.multiplier = 10 ** 2   # 默认保留2位小数
        self.setSingleStep(1 / self.multiplier)    # 步长跟随保留位数设置
        self.valueChanged.connect(self.on_value_changed)
        self.rangeChanged.connect(self.on_range_changed)

    def float_2_int(self, p_float):
        return int(Decimal(p_float * self.multiplier))

    def on_value_changed(self, value):
        self.floatValueChanged.emit(value / self.multiplier)

    def on_range_changed(self, min, max):
        self.floatRangeChanged.emit(min / self.multiplier, max / self.multiplier)

    def setRange(self, p_float, p_float_1):
        super(QDoubleSlider, self).setRange(self.float_2_int(p_float), self.float_2_int(p_float_1))

    def setValue(self, p_float):
        super(QDoubleSlider, self).setValue(self.float_2_int(p_float))

    def setMinimum(self, p_float):
        super(QDoubleSlider, self).setMinimum(self.float_2_int(p_float))

    def setMaximum(self, p_float):
        super(QDoubleSlider, self).setMaximum(self.float_2_int(p_float))

    def setSingleStep(self, p_float):
        """必须在setRange之前调用"""
        self.multiplier = int(1 / p_float)
        super(QDoubleSlider, self).setSingleStep(self.float_2_int(p_float))

    def minimum(self):
        return super(QDoubleSlider, self).minimum() / self.multiplier

    def maximum(self):
        return super(QDoubleSlider, self).maximum() / self.multiplier

    def value(self):
        return super(QDoubleSlider, self).value() / self.multiplier

    def singleStep(self):
        return super(QDoubleSlider, self).singleStep() / self.multiplier


class MyWnd(QWidget):
    def __init__(self, parent=None):
        super(MyWnd, self).__init__(parent)
        self.resize(300, 200)
        layout_v = QVBoxLayout()
        self.slider = QDoubleSlider(Qt.Horizontal, self)
        self.label_value = QLabel(self)
        layout_v.addWidget(self.slider)
        layout_v.addWidget(self.label_value)
        self.setLayout(layout_v)

        self.slider.floatValueChanged.connect(self.on_value_changed)
        self.slider.floatRangeChanged.connect(self.on_range_changed)
        self.slider.setSingleStep(0.0001)   # 必须在setRange之前调用
        self.slider.setRange(0.0001, 10.0001)
        self.slider.setValue(0.1516)

    def on_value_changed(self, value):
        self.label_value.setText("范围({},{}), 步长{}, 当前{}".format(
            self.slider.minimum(), self.slider.maximum(), self.slider.singleStep(), value))

    def on_range_changed(self, value, value2):
        print(value, value2)


if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    wnd = MyWnd()
    wnd.show()
    sys.exit(app.exec_())
